package com.bigtotoro.shuiyincamera.video;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bigtotoro.shuiyincamera.R;
import com.bigtotoro.shuiyincamera.ShowMyArtsActivity;
import com.bigtotoro.shuiyincamera.model.MyArt;
import com.bigtotoro.shuiyincamera.util.VideoItem;
import com.bigtotoro.shuiyincamera.util.VideoUtil;
import com.bigtotoro.shuiyincamera.video.filter.FilterType;
import com.daasuu.gpuv.composer.FillMode;
import com.daasuu.gpuv.composer.GPUMp4Composer;
import com.daasuu.gpuv.egl.filter.GlFilter;
import com.daasuu.gpuv.egl.filter.GlFilterGroup;
import com.daasuu.gpuv.egl.filter.GlMonochromeFilter;
import com.daasuu.gpuv.egl.filter.GlVignetteFilter;

import qiu.niorgai.StatusBarCompat;

import static com.bigtotoro.shuiyincamera.editimage.Constacts.EXTRA_OUTPUT;

public class VideoProgressActivity extends AppCompatActivity {
    public static final String TAG = "video-progress";
    GPUMp4Composer gPUMp4Composer;
    private ProgressBar mProgressDialog;
    private TextView barText;
    private VideoItem videoItem;
    private String inPath = null, outPath = null;
    private GlFilter glFilter = new GlFilterGroup(new GlMonochromeFilter(), new GlVignetteFilter());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_progress);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.black));
        int position = getIntent().getIntExtra("position", 0);
        inPath = getIntent().getStringExtra("path");
        videoItem = VideoUtil.createVideoItem(inPath);
        final FilterType[] filters = FilterType.values();
        glFilter = null;
        glFilter = FilterType.createGlFilter(filters[position], this);
        mProgressDialog = findViewById(R.id.bar);
        barText = findViewById(R.id.bar_text);

        save();
    }



    private void save(){
        outPath = VideoUtil.VIDEO_OUT_DIR+"/"+System.currentTimeMillis()+".mp4";
        mProgressDialog.setMax(100);
        mProgressDialog.setProgress(0);
//        findViewById(R.id.start_play_movie).setEnabled(false);

        gPUMp4Composer = null;
        gPUMp4Composer = new GPUMp4Composer(videoItem.getPath(), outPath)
                // .rotation(Rotation.ROTATION_270)
                //.size(720, 720)
                .fillMode(FillMode.PRESERVE_ASPECT_CROP)
                .filter(glFilter)
//                .mute(muteCheckBox.isChecked())
//                .flipHorizontal(flipHorizontalCheckBox.isChecked())
//                .flipVertical(flipVerticalCheckBox.isChecked())
                .listener(new GPUMp4Composer.Listener() {
                    @Override
                    public void onProgress(double progress) {
                        Log.d(TAG, "onProgress = " + progress);
                        runOnUiThread(() -> {
                            barText.setText((int) (progress * 100)+"%");
                            mProgressDialog.setProgress((int) (progress * 100));
                        });
                    }

                    @Override
                    public void onCompleted() {
                        Log.d(TAG, "onCompleted()");
//                        exportMp4ToGallery(getApplicationContext(), outPath);
                        runOnUiThread(() -> {
                            barText.setText("100%");
                            mProgressDialog.setProgress(100);

                            Toast.makeText(VideoProgressActivity.this, "保存成功", Toast.LENGTH_SHORT).show();

                            MyArt art = new MyArt();
                            art.setHeight(videoItem.getHeight());
                            art.setWidth(videoItem.getWidth());
                            art.setCreate_time(System.currentTimeMillis() + "");
                            art.setPath(outPath);
                            art.setType(1);
                            art.save();

                            Intent intent = new Intent(VideoProgressActivity.this, ShowMyArtsActivity.class);
                            intent.putExtra(EXTRA_OUTPUT, outPath);
                            intent.putExtra("create_time", art.getTrueCreateTime());
                            intent.putExtra("width", videoItem.getWidth());
                            intent.putExtra("height", videoItem.getHeight());
                            intent.putExtra("type", "1");
                            startActivity(intent);
                            finish();
                        });
                    }

                    @Override
                    public void onCanceled() {
                    }

                    @Override
                    public void onFailed(Exception exception) {
                        Log.e(TAG, "onFailed() "+exception.getMessage());
                    }
                })
                .start();
    }
}
